<template>
  <div class="drag-line-view">
    <div v-for="(item, index) in dataArr" :key="index" class="item">
      <div class="label">{{ item.label }}</div>
      <div class="list">
        <div
          v-for="(data, i) in lineList"
          :key="data"
          class="list-item"
          :class="{ last: lineList.length - 1 === i }"
        ></div>
        <drag-line-item v-model="item.value"></drag-line-item>
        <div v-if="index === dataArr.length - 1" class="stick-wrap">
          <div v-for="(d, num) in stickList" :key="num" class="item">
            <div class="text">{{ d }}</div>
          </div>
        </div>
      </div>
      <div class="options common-flex-align-center">
        <t-button class="common-button" theme="primary" @click="handleCopy(index)">
          <template #icon><svg-icon class="icon" name="copy"></svg-icon></template>
          复制
        </t-button>
        <t-button class="common-button" variant="outline" @click="handleClear(index)">
          <template #icon><svg-icon class="icon" name="clear"></svg-icon></template>
          清空
        </t-button>
      </div>
    </div>
    <copy-dialog ref="copyDialogRef" :index="copyIndex" @sure="handleCopySure"></copy-dialog>
  </div>
</template>

<script setup lang="ts">
import _ from 'lodash';
import { onMounted, ref } from 'vue';

import CopyDialog from './CopyDialog.vue';
import DragLineItem from './DragLineItem.vue';

const copyDialogRef = ref();
const copyIndex = ref(0);

const dayArr = ['一', '二', '三', '四', '五', '六', '天'];
const dataArr = ref<any>([]);
const lineList = ref<any>([]);
const stickList = ref<any>([]);

const initLineList = () => {
  dataArr.value = Array(7)
    .fill('')
    .map((_: any, index: number) => {
      return { label: `星期${dayArr[index]}`, value: [{ s: '11:00', e: '14:00' }] };
    });

  lineList.value = Array(24).fill('');

  stickList.value = Array(25)
    .fill('')
    .map((_: any, index: number) => {
      return `${index < 10 ? `0${index}` : index}:00`;
    });
};

const handleCopy = (index: number) => {
  copyIndex.value = index;
  copyDialogRef.value.open();
};

const handleCopySure = (arr: any[]) => {
  const targetData = _.cloneDeep(dataArr.value[copyIndex.value].value);
  dataArr.value = dataArr.value.map((item: any, index: number) => {
    if (arr.includes(index)) item.value = targetData;
    return item;
  });
};

const handleClear = (num: number) => {
  dataArr.value = dataArr.value.map((item: any, index: number) => {
    if (num === index) item.value = [];
    return item;
  });
};

onMounted(() => {
  initLineList();
});
</script>

<style lang="less" scoped>
.drag-line-view {
  padding-top: 20px;

  .item {
    display: flex;
    align-items: center;

    & + .item {
      .list {
        border-top: none;
      }
    }

    .label {
      font-size: 12px;
      color: #333;
      margin-right: 12px;
      flex-shrink: 0;
    }

    .list {
      border: 2px dashed rgb(160 174 192 / 60%);
      display: flex;
      align-items: center;
      position: relative;

      .list-item {
        width: 58px;
        height: 40px;
        flex-shrink: 0;

        & + .list-item {
          border-left: 2px dashed rgb(160 174 192 / 60%);
        }
      }

      .stick-wrap {
        width: 100%;
        position: absolute;
        left: 0;
        bottom: -46px;
        display: flex;
        align-items: center;

        .item {
          width: 58px;
          height: 46px;
          position: relative;
          flex-shrink: 0;

          .text {
            position: absolute;
            left: -50%;
            top: 50%;
            transform: translate(50%, -50%);
            font-size: 12px;
            color: #a5b4cb;
          }
        }
      }
    }

    .options {
      margin-left: 24px;

      .icon {
        margin-right: 5px;
      }
    }
  }
}
</style>
